package com.slz3.business.module.orderManagement.ordRefund.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.orderManagement.ordRefund.domain.OrdRefund;
import com.slz3.business.module.orderManagement.ordRefund.domain.OrdRefundFactory;
import com.slz3.business.module.orderManagement.ordRefund.steward.OrdRefundSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class OrdRefundServiceImpl implements OrdRefundService {

    @Override
    public OrdRefund save(OrdRefund ordRefund) {
        return Repositorys.ordRefund.save(OrdRefundFactory.initBaseFields(ordRefund));
    }

    @Override
    public Page<OrdRefund> page(MultiValueMap<String, String> params) {
        Page<OrdRefund> ordRefundPage = Repositorys.ordRefund.findAll(
                OrdRefundSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return ordRefundPage;
    }

    @Override
    public OrdRefund modify(OrdRefund ordRefundNew) {
        return Repositorys.ordRefund.saveAndFlush(OrdRefundFactory.initBaseFields(ordRefundNew));
    }

    @Override
    public Optional<OrdRefund> single(UUID uuid) {
        return Repositorys.ordRefund.findById(uuid);
    }

    @Override
    public List<OrdRefund> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.ordRefund.findAllById(uuids);
    }

    @Override
    public List<OrdRefund> delete(List<OrdRefund> ordRefundList) {
        ordRefundList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            OrdRefundFactory.initBaseFields(item);
        });
        return Services.ordRefundTransactional.batchModify(ordRefundList);
    }

}
